package com.woniuxy.woniuhis.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.woniuhis.entity.ExaminationName;
import com.woniuxy.woniuhis.entity.ExaminationResult;
import com.woniuxy.woniuhis.entity.Medicine;
import com.woniuxy.woniuhis.entity.Patient;
import com.woniuxy.woniuhis.service.ExaminationService;
import com.woniuxy.woniuhis.util.ResponseJson;

@Controller
@RequestMapping("/examination")
public class ExaminationController {
	
	@Autowired
	ExaminationService examinationService;
	

	@RequestMapping("/allExaminationName")
	@ResponseBody
	public ResponseJson<ExaminationName> allExaminationName(){
		ResponseJson<ExaminationName> responseJson = null;
		try {
			responseJson=examinationService.findAllExaminationNames();
			return responseJson;
		} catch (Exception e) {
			responseJson.setCode(1);
			responseJson.setMsg(e.getMessage());
			return responseJson;
		}
	}
	
	
	@RequestMapping("/searchExaminationName")
	@ResponseBody
	public ResponseJson<ExaminationName> searchExaminationName(@RequestBody ExaminationName examinationName){
		ResponseJson<ExaminationName> responseJson=null;
		try {
			responseJson = examinationService.findAllExaminationNamesByCondition(examinationName);
			return responseJson;
		} catch (Exception e) {
			responseJson.setCode(1);
			responseJson.setMsg(e.getMessage());
			return responseJson;
		}
	}
	
	//生成检查单，并且生成多条检查条目。并返回检查单号
	@RequestMapping("/addExamination")
	@ResponseBody
	public String addExamination(@RequestParam("examNameIds") String examNameIds,
			@RequestParam("outpatientId") String outpatientId) {
		String examId = null;
		try {
			examId = examinationService.addExamination(examNameIds,outpatientId);
			return examId;
		} catch (Exception e) {
			e.printStackTrace();
			return "系统维护中";
		}
	}
	
	//根据Examination的id查询该检查单的检查项的所有检查结果
	@RequestMapping("/findAllExamResultByExamId")
	@ResponseBody
	public ResponseJson<ExaminationResult> findAllExamResultByExamId(@RequestParam("examinationId") String examinationId){
		ResponseJson<ExaminationResult> responseJson = null;
		try {
			responseJson = examinationService.findAllExamResultByExamId(examinationId);
			return responseJson;
		} catch (Exception e) {
			responseJson.setCode(1);
			responseJson.setMsg(e.getMessage());
			return responseJson;
		}
	}
	
	//查询所有处方药名
	@RequestMapping("/allMedicine")
	@ResponseBody
	public ResponseJson<Medicine> allMedicine(){
		ResponseJson<Medicine> responseJson=null;
		try {
			responseJson = examinationService.findAllMedicine();
			return responseJson;
		} catch (Exception e) {
			responseJson.setCode(1);
			responseJson.setMsg(e.getMessage());
			return responseJson;
		}
	}
	//根据药物名称搜索药物
	@RequestMapping("/searchMedicine")
	@ResponseBody
	public ResponseJson<Medicine> searchMedicine(@RequestBody Medicine medicine){
		ResponseJson<Medicine> responseJson=null;
		try {
			responseJson = examinationService.findAllMedicineByMedicineName(medicine);
			return responseJson;
		} catch (Exception e) {
			responseJson.setCode(1);
			responseJson.setMsg(e.getMessage());
			return responseJson;
		}
	}
	//添加一个处方药单，相应生成处方药条目
	@RequestMapping("/addRecipeMedi")
	@ResponseBody
	public String addRecipeMedi(@RequestParam("mediIds") String mediIds,
			@RequestParam("outpatientId") String outpatientId) {
		//生成处方药单和处方药项，并返回处方药id
		String recipeMediId = null;
		try {
			recipeMediId = examinationService.addRecipeMedi(mediIds,outpatientId);
			return "保存成功";
		} catch (Exception e) {
			e.printStackTrace();
			return "系统维护中";
		}
		
	}
	
	
}
